package com.zhongwang.mms.module.basic.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.common.ValidResult;
import com.zhongwang.mms.gen.entity.TDictionary;
import com.zhongwang.mms.module.basic.service.TDictionaryService;
import com.zhongwang.mms.module.system.log.LogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * @Description: 数据字典维护 控制层
 * @Author: China.wsg
 * @Date: 2020/05/12 09:43
 */
@Controller
@RequestMapping("/basic/dictionary")
public class TDictionaryController extends BaseController {

    @Autowired
    private TDictionaryService dictionaryService;
    @Autowired
    private LogService logService;


    /**
     * 数据字典 页面
     *
     * @return 页面路径
     */
    @GetMapping("/page")
    @RequiresPermissions({"basic:info:view"})
    public String page() {
        return "basic/dictionary/page";
    }

    /**
     * 数据字典列表
     *
     * @return 数据字典 Json 数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"basic:info:view"})
    public List<TDictionary> list(TDictionary dictionary) {
        return dictionaryService.findDictionary(dictionary);
    }

    /**
     * 创建字典
     *
     * @param dictionary 字典信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"basic:info:edit"})
    public JsonResult save(TDictionary dictionary) {
        logService.saveLog("新建数据字典", dictionary.getDicName());
        return jsonResult(dictionaryService.save(dictionary));
    }

    /**
     * 名称唯一验证
     *
     * @param r 数据字典
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    public ValidResult checkUnique(TDictionary r) {
        TDictionary dictionary = dictionaryService.findDictionaryByName(r.getDicName());
        if (r.getDicId() == null) {
            return ValidResult.result(dictionary);
        }
        if (dictionary == null || dictionary.getDicId().equals(r.getDicId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 删除字典
     *
     * @param dictionary 字典信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"basic:info:edit"})
    public JsonResult del(TDictionary dictionary) {
        return jsonResult(dictionaryService.del(dictionary));
    }

    /**
     * 查询字典
     *
     * @param id 字典id
     * @return 字典
     */
    @ResponseBody
    @PostMapping("/id")
    @RequiresPermissions({"basic:info:edit"})
    public TDictionary id(Integer id) {
        return dictionaryService.findDictionaryById(id);
    }

    /**
     * 修改字典
     *
     * @param dictionary 字典信息
     * @return json 数据
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"basic:info:edit"})
    public JsonResult update(TDictionary dictionary) {
        TDictionary r = dictionaryService.findDictionaryById(dictionary.getDicId());
        logService.saveLog("修改数据字典", r, dictionary);
        return jsonResult(dictionaryService.updateDictionary(dictionary));
    }


}
